﻿using Arction.Wpf.BindableCharting;
using Arction.Wpf.BindableCharting.SeriesXY;
using Arction.Wpf.BindableCharting.Titles;
using ReactiveUI;
using ReactiveUI.Fody.Helpers;
using System;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Timers;
using System.Windows.Media;

namespace LightningChartBindingExample
{
    public class SeriesViewModel : ReactiveObject, INotifyPropertyChanged
    {

        private Timer _timer;


        [Reactive]
        public SeriesPointCollection DataPoints { get;set; }

        [Reactive]
        public LineStyle LineStyle { get; set; }

        [Reactive]
        public SeriesTitle Title { get; set; }

        [Reactive]
        public SeriesPointCollection DataPoints1 { get; set; }

        public string Name { get; set; }

        public SeriesViewModel(string name,int time)
        {
            Name = name;
            DataPoints = new SeriesPointCollection();
            DataPoints1 = new SeriesPointCollection();
            LineStyle = new LineStyle();
            LineStyle.Color = Color.FromRgb(244, 22, 111);
            Title = new SeriesTitle();
            Title.Text = "你好哈哈哈哈";
            Title.Visible = true;
            _timer = new Timer(time);
            _timer.Elapsed += (s, e) => UpdateData();
            _timer.Start();
        }

        int j = 0;
        private void UpdateData()
        {
            App.Current.Dispatcher.Invoke(() =>
            {
                // 添加新数据点，并限制数据点数量
                // 添加多个新数据点
                for (int i = 0; i < 3; i++) // 每次添加 5 个数据点
                {
                    DataPoints.Add(new SeriesPoint() { X = j++, Y = new Random().NextDouble() * 10,PointColor = Color.FromRgb(244,22,111)});
                    DataPoints1.Add(new SeriesPoint() { X = j++, Y = new Random().NextDouble() * 10 - 5, PointColor = Color.FromRgb(244, 22, 111) });
                }

                if (DataPoints.Count > 100)
                {
                    DataPoints.RemoveAt(0); // 保留最新的 100 个数据点
                    DataPoints1.RemoveAt(0); // 保留最新的 100 个数据点
                }
            });
        }
    }
}
